<?php
namespace app\admin\controller;
use think\Validate;
use think\Db;
use think\Session;

class StallController extends BaseController
{
    public function lists()
    {
        // 团队人员不能看档口
        // $account_id = Session::get('uid');
        // $account_member = Db::name('account_member')->where('account_id',$account_id)->find();
        // if(!empty($account_member)){
        //     $this->error('没有权限访问');
        // }
        return view();
    }
     public function getList()
    {  
        $where = array_filter(input('post.'));
        if (empty($where['parent_id'])) {
            $where['parent_id'] = ['>',0];
        }
        if (!empty($where['name'])) {
            $where['name'] = ['like','%'.$where['name'].'%'];
        }
        if (!empty($where['page_size'])) {
            $page_size = $where['page_size'];
            unset($where['page_size']);
        }else{
            $page_size = 15;
        }


        $page = max(input('get.page',0),1);
        // $page_size = 15;
        
        $total = Db::name('stall')->where($where)->count();
        $data = [
            'rows'      => [],
            'total_page'=> ceil($total/$page_size),
            'page_size' => $page_size,
            'page'      => $page
        ];
        if ($total == 0) {
            $this->success('无记录','',$data);
        }

        $rows = Db::name('stall')->where($where)->field('parent_id', true)
                                    ->order('parent_id')->page($page, $page_size)->select();
        //echo Area::getlastsql();exit;
        foreach ($rows as $key => $row) {
            $arr = explode(',', $row['name_path']);
            $rows[$key]['pos'] = $arr[0];
            $rows[$key]['stall'] = $arr[1];
            $rows[$key]['add_time'] = date('Y-m-d H:i',$row['add_time']);
        }

        $data['rows'] = $rows;
        $this->success('成功','',$data);
    }
    public function getTopList()
    {
        $rows = Db::name('stall')->where('parent_id', 0)->field('parent_id', true)->select();
        $data['rows'] = $rows;
        $this->success('成功','',$data);
    }

    public function handle()
    {
        $data = input('post.');
        //数据验证
        $validate = new Validate([
            ['parent_id','require','请选择档口位置'],
            ['name','require','地区名称必须'],
        ]);
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        //判断是否重复
        if ( Db::name('stall')->where('name', $data['name'])->find() ) {
            $this->error('档口已存在');
        }

        if ($data['parent_id'] == 0) {
            $data['name_path'] = $data['name'];
            $data['level'] = 1;
        } else {
            $parent_name = Db::name('stall')->where('id',$data['parent_id'])->value('name');
            $data['name_path'] = $parent_name . ',' . $data['name'];
            $data['level'] = 2;
        }
        $data['add_time'] = time();
        if (Db::name('stall')->insert($data)) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }
}